#!/bin/bash

. /etc/profile
. ~/.bash_profile
. ~/.bashrc
WORK_DIR=${PWD}

if [ -z "$1" ]
then
	BRANCH="master"
else
	BRANCH=$1
fi

rm -r ${WORK_DIR}/.repo
rm -rf ${WORK_DIR}/${BRANCH}
until [ $? -eq 0 ]
do
	rm -rf ${WORK_DIR}/${BRANCH}
	sleep 5s
done
	echo "remove successfully"

mkdir -p ${WORK_DIR}/${BRANCH}
cd ${WORK_DIR}/${BRANCH}
repo init -u https://gitee.com/OpenHarmony/manifest.git -b master -m llvm-toolchain.xml

repo sync -c
timeout 10s repo sync -c > ${WORK_DIR}/repo.log

until grep -q "finished successfully" ${WORK_DIR}/repo.log
do
	echo "repo sync retry ..." >> ${WORK_DIR}/repo.log
	rm -rf ./*
	repo sync -c
	timeout 10s repo sync -c >> ${WORK_DIR}/repo.log
	sleep 10s
done
	echo "repo sync has finished successfully"

repo forall -c 'git lfs pull'

rm -rf ${WORK_DIR}/${BRANCH}/out
${WORK_DIR}/${BRANCH}/toolchain/llvm-project/llvm-build/env_prepare.sh > ${WORK_DIR}/env_prepare.log 2>&1

export PATH=${WORK_DIR}/${BRANCH}/prebuilts/python3/darwin-x86/3.11.4/bin:$PATH
cd ${WORK_DIR}/${BRANCH}/toolchain/llvm-project
git fetch origin ${BRANCH}
git checkout origin/${BRANCH}
cd ${WORK_DIR}/${BRANCH}

/usr/bin/python3 ${WORK_DIR}/${BRANCH}/toolchain/llvm-project/llvm-build/build.py --strip --build-lldb-static --build-python --build-ncurses --build-libedit --build-libxml2 --lldb-timeout --compression-format gz --no-strip-libs --build-with-debug-info --enable-lzma-7zip > ${WORK_DIR}/llvm_build.log 2>&1
#echo | mutt -a llvm_build.log -s "llvm build log" -- zhaoxinyuan9@huawei.com,liuyaning7@huawei.com
